<?php

/*
Copyright 2009-2011 Sam Weiss
All Rights Reserved.

This file is part of Spark/Plug.

Spark/Plug is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

if (!defined('spark/plug'))
{
	header('HTTP/1.1 403 Forbidden');
	exit('<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>403 Forbidden</title></head><body><h1>Forbidden</h1><p>You don\'t have permission to access the requested resource on this server.</p></body></html>');
}

//-------------------------------------------------------------------------------------

class _BlogModel extends SparkModel
{
	private $db;
	
	//-------------------------------------------------------------------------------------

	public function __construct()
	{
		parent::__construct();
		
		// quick hack to initialize the database if it does not yet exist
		// for sample code only - NOT a suitable technique for use in production code
		
		try
		{
			$this->db = $this->loadDB();
			@$this->db->countRows('entries');
		}
		catch (Exception $e)
		{
			$this->initDB();
		}
	}

	//-------------------------------------------------------------------------------------

	private function initDB()
	{
		$ct = $this->db->getFunction('create_table');

		$this->db->query('DROP TABLE IF EXISTS {entries}');
		$ct->table('entries');
		$ct->field('id', iSparkDBQueryFunctionCreateTable::kFieldTypeInteger, NULL, NULL, false, iSparkDBQueryFunctionCreateTable::kFlagPrimaryKey | iSparkDBQueryFunctionCreateTable::kFlagAutoincrement);
		$ct->field('title', iSparkDBQueryFunctionCreateTable::kFieldTypeString, 128);
		$ct->field('body', iSparkDBQueryFunctionCreateTable::kFieldTypeText);
		$this->db->query($ct->compile());

		$this->db->query('DROP TABLE IF EXISTS {comments}');
		$ct->table('comments');
		$ct->field('id', iSparkDBQueryFunctionCreateTable::kFieldTypeInteger, NULL, NULL, false, iSparkDBQueryFunctionCreateTable::kFlagPrimaryKey | iSparkDBQueryFunctionCreateTable::kFlagAutoincrement);
		$ct->field('author', iSparkDBQueryFunctionCreateTable::kFieldTypeString, 128);
		$ct->field('body', iSparkDBQueryFunctionCreateTable::kFieldTypeText);
		$ct->field('entry_id', iSparkDBQueryFunctionCreateTable::kFieldTypeInteger, NULL, 0);
		$ct->foreignKey('entry_id', 'entries', 'id', array(iSparkDBQueryFunctionCreateTable::kForeignKeyTriggerDelete=>iSparkDBQueryFunctionCreateTable::kForeignKeyActionCascade, iSparkDBQueryFunctionCreateTable::kForeignKeyTriggerUpdate=>iSparkDBQueryFunctionCreateTable::kForeignKeyActionCascade));
		$this->db->query($ct->compile());

		$this->db->insertRow('entries', array('title'=>'My First Entry', 'body'=>'Yadda yadda yadda'));
		$this->db->insertRow('entries', array('title'=>'My Second Entry', 'body'=>'Some more text!'));
	}

	//-------------------------------------------------------------------------------------

	public function getEntries()
	{
		return $this->db->selectRows('entries');
	}

	//-------------------------------------------------------------------------------------

	public function getComments($entry_id)
	{
		return $this->db->selectRows('comments', '*', 'entry_id=?', $entry_id);
	}

	//-------------------------------------------------------------------------------------

	public function insertComment($comment)
	{
		return $this->db->insertRow('comments', $comment);
	}

	//-------------------------------------------------------------------------------------
}
